package com.zj.leetcode.hard.listNode;

/**
 * @program: algorithm
 * @description: 合并K个升序链表
 * 23 merge-k-sorted-lists
 * <p>
 * 输入：lists = [[1,4,5],[1,3,4],[2,6]]
 * 输出：[1,1,2,3,4,4,5,6]
 * 解释：链表数组如下：
 * [
 * 1->4->5,
 * 1->3->4,
 * 2->6
 * ]
 * 将它们合并到一个有序链表中得到。
 * 1->1->2->3->4->4->5->6
 * <p>
 * 来源：力扣（LeetCode）
 * 链接：https://leetcode-cn.com/problems/merge-k-sorted-lists
 * 著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。
 * @author: Zhang Bo
 * @create: 2022-01-14 17:05
 **/
public class MergeKSortedLists {
    public static void main(String[] args) {
        ListNode l1 = new ListNode(1,
                new ListNode(4,
                        new ListNode(5, null)));

        ListNode l2 = new ListNode(1,
                new ListNode(3,
                        new ListNode(4, null)));

        ListNode l3 = new ListNode(2,
                new ListNode(6, null));

        ListNode[] lists = new ListNode[]{l1, l2, l3};

        Solution solution = new MergeKSortedLists().new Solution();

        System.out.println(solution.mergeKLists01(lists));
    }
    //leetcode submit region begin(Prohibit modification and deletion)

    /**
     * Definition for singly-linked list.
     * public class ListNode {
     * int val;
     * ListNode next;
     * ListNode() {}
     * ListNode(int val) { this.val = val; }
     * ListNode(int val, ListNode next) { this.val = val; this.next = next; }
     * }
     */

    public static class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }

        @Override
        public String toString() {
            return "ListNode{" +
                    "val=" + val +
                    ", next=" + next +
                    '}';
        }
    }

    class Solution {
        public ListNode mergeKLists(ListNode[] lists) {

            int size = lists.length;

            if (size <= 0) {
                return null;
            }

            if (size == 1) {
                return lists[0];
            }

            ListNode tempNode = lists[0];
            for (int i = 1; i < size; i++) {
                tempNode = this.mergeNode(tempNode, lists[i]);
            }
            return tempNode;
        }

        public ListNode mergeKLists01(ListNode[] lists) {
            return merge(lists, 0, lists.length - 1);
        }

        public ListNode merge(ListNode[] lists, int l, int r) {

            if (l == r) {
                return lists[l];
            }
            if (l > r) {
                return null;
            }
            int mid = (r - l) >> 1 + l;
            return this.mergeNode(merge(lists, l, mid), merge(lists, mid + 1, r));
        }

        public ListNode mergeNode(ListNode l1, ListNode l2) {
            if (l1 == null || l2 == null) {
                return l1 != null ? l1 : l2;
            }
            ListNode temp = new ListNode(-1);
            ListNode cur = temp, aPtr = l1, bPtr = l2;
            while (aPtr != null && bPtr != null) {
                if (aPtr.val <= bPtr.val) {
                    cur.next = aPtr;
                    aPtr = aPtr.next;
                } else {
                    cur.next = bPtr;
                    bPtr = bPtr.next;
                }
                cur = cur.next;
            }
            cur.next = aPtr == null ? bPtr : aPtr;

            return temp.next;
        }


    }
//leetcode submit region end(Prohibit modification and deletion)

}
